package admin;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class ModifyBoardAction extends DispatchAction{
	
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		ModifyBoardForm modifyform = (ModifyBoardForm)form;
		
		setting(modifyform);
		
		return mapping.findForward("form");
	}
	
	public ActionForward category(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		ModifyBoardForm modifyform = (ModifyBoardForm)form;
		
		String communityName;
		String searchCono = modifyform.getSearchCono(); //커뮤니티 코드 취득
		int cono = Integer.parseInt(searchCono);
				
		CommunityDAO communityDao = new CommunityDAO();		//community 테이블 매칭 DAO
		NoticeBoardDAO noticeDao = new NoticeBoardDAO();	//notice DAO
		FreeBoardDAO freeDao = new FreeBoardDAO();			//free DAO
		DataBoardDAO dataDao = new DataBoardDAO();			//data DAO
		PhotoBoardDAO photoDao = new PhotoBoardDAO();		//photo DAO
		
		//---------------------------------------------
		long totalCount;
		long coNoticeCount;		//공지사항(동호회용)
		long coFreeCount;		//자유게시판(동호회용)
		long coDataCount;		//자료게시판(동호회용)
		long coPhotoCount;		//앨범게시판(동호회용)
		String coDisplayInfo;	//검색된 동호회의 기본정보 
		
		//******************************필요테이터취득
		
		//default setting
		setting(modifyform);
		
		long allBoardCount = Long.parseLong(modifyform.getAllBoardCount());
		
		coNoticeCount = noticeDao.getTotalRows(cono);
		coFreeCount = freeDao.getTotalRows(cono);
		coDataCount = dataDao.getTotalRows(cono);
		coPhotoCount = photoDao.getTotalRows(cono);
		communityName = communityDao.getCommunityName(cono);
		
		totalCount = coNoticeCount + coFreeCount + coDataCount + coPhotoCount ;
		coDisplayInfo = communityName + "는 현재 " + totalCount + "(전체의"+getPercent(totalCount , allBoardCount)+"%)건을 사용중입니다." ;
		
		modifyform.setSearchCono(""+cono);
		modifyform.setCoDataCount(""+coDataCount);
		modifyform.setCoDisplayInfo(""+coDisplayInfo);
		modifyform.setCoFreeCount(""+coFreeCount);
		modifyform.setCoNoticeCount(""+coNoticeCount);
		modifyform.setCoPhotoCount(""+coPhotoCount);
		modifyform.setCoDisplayInfo(coDisplayInfo);
		modifyform.setCommunityInfo(communityDao.getSearchCommunityList());	//검색용 정보
		//******************************완료
		
		return mapping.findForward("form");
	}
	
	private void setting(ModifyBoardForm modifyform)
	{
		long allBoardCount;	//전체게시판
		long noticeCount;	//공지사항
		long qnaCount;		//Q&A게시판
		long freeCount;		//자유게시판
		long dataCount;		//자료게시판
		long photoCount;	//앨범게시판
		
		CommunityDAO communityDao = new CommunityDAO();		//community 테이블 매칭 DAO
		NoticeBoardDAO noticeDao = new NoticeBoardDAO();	//notice DAO
		QnaBoardDAO qnaDao = new QnaBoardDAO();				//qna DAO
		FreeBoardDAO freeDao = new FreeBoardDAO();			//free DAO
		DataBoardDAO dataDao = new DataBoardDAO();			//data DAO
		PhotoBoardDAO photoDao = new PhotoBoardDAO();		//photo DAO
		
		//******************************필요테이터취득
		noticeCount = noticeDao.getTotalRows();
		qnaCount = qnaDao.getTotalRows();
		freeCount = freeDao.getTotalRows();
		dataCount = dataDao.getTotalRows();
		photoCount = photoDao.getTotalRows();
		
		allBoardCount = noticeCount + qnaCount + freeCount + dataCount + photoCount;
		
		//===============================================================================
		modifyform.setAllBoardCount(""+allBoardCount);
		modifyform.setDataCount(""+dataCount +"("+getPercent(dataCount, allBoardCount)+"%)");
		modifyform.setFreeCount(""+freeCount +"("+getPercent(freeCount, allBoardCount)+"%)");
		modifyform.setNoticeCount(""+noticeCount +"("+getPercent(noticeCount, allBoardCount)+"%)");
		modifyform.setPhotoCount(""+photoCount +"("+getPercent(photoCount, allBoardCount)+"%)");
		modifyform.setQnaCount(""+qnaCount +"("+getPercent(qnaCount, allBoardCount)+"%)");
		modifyform.setCommunityInfo(communityDao.getSearchCommunityList());	//검색용 정보
		//******************************완료
	}
	
	public Long getPercent(long tagetVal , long totalVal)
	{
		double per ;
		long resultPercent = 0;
		
		if(tagetVal != 0)
		{
			Double tagetValD = new Double(tagetVal);
			Double totalValD = new Double(totalVal);
			
			Double result = (tagetValD/totalValD)*100;
			
			//소스점을 없애버린다.
			resultPercent = Math.round(Math.ceil(result));
		}
		return resultPercent;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		ModifyBoardForm modifyform = (ModifyBoardForm)form;
		
		//--------------------------------------------------------------------------------------
		String communityName;
		String searchCono = modifyform.getSearchCono(); //커뮤니티 코드 취득
		int cono = Integer.parseInt(searchCono);
		
		
		CommunityDAO communityDao = new CommunityDAO();		//community 테이블 매칭 DAO
		NoticeBoardDAO noticeDao = new NoticeBoardDAO();	//notice DAO
		FreeBoardDAO freeDao = new FreeBoardDAO();			//free DAO
		DataBoardDAO dataDao = new DataBoardDAO();			//data DAO
		PhotoBoardDAO photoDao = new PhotoBoardDAO();		//photo DAO
		CommentDAO commentDao = new CommentDAO();			//commentDAO
		
		//-----------------------------------------------------------------------삭제작업
		String func = req.getParameter("func");
		
	/*	System.out.println(func);
		System.out.println(cono);*/
		
		if(func.equals("free"))
		{
			commentDao.FreeCommentdelete(cono);
			freeDao.delete(cono);
		}
		else if(func.equals("photo"))
		{
			commentDao.PhotoCommentdelete(cono);
			photoDao.delete(cono);
		}
		else if(func.equals("data"))
		{
			commentDao.DataCommentdelete(cono);
			dataDao.delete(cono);
		}
		//----------------------------------------------------------------------삭제작업완료
		
		
		//---------------------------------------------
		long totalCount;
		long coNoticeCount;		//공지사항(동호회용)
		long coFreeCount;		//자유게시판(동호회용)
		long coDataCount;		//자료게시판(동호회용)
		long coPhotoCount;		//앨범게시판(동호회용)
		String coDisplayInfo;	//검색된 동호회의 기본정보 
		
		//******************************필요테이터취득
		//default setting
		setting(modifyform);
		long allBoardCount = Long.parseLong(modifyform.getAllBoardCount());
		
		coNoticeCount = noticeDao.getTotalRows(cono);
		coFreeCount = freeDao.getTotalRows(cono);
		coDataCount = dataDao.getTotalRows(cono);
		coPhotoCount = photoDao.getTotalRows(cono);
		communityName = communityDao.getCommunityName(cono);
		
		totalCount = coNoticeCount + coFreeCount + coDataCount + coPhotoCount ;
		coDisplayInfo = communityName + "는 현재 " + totalCount + "(전체의"+getPercent(totalCount , allBoardCount)+"%)건을 사용중입니다." ;
		modifyform.setCoDataCount(""+coDataCount);
		modifyform.setCoDisplayInfo(""+coDisplayInfo);
		modifyform.setCoFreeCount(""+coFreeCount);
		modifyform.setCoNoticeCount(""+coNoticeCount);
		modifyform.setCoPhotoCount(""+coPhotoCount);
		modifyform.setCoDisplayInfo(coDisplayInfo);
		modifyform.setCommunityInfo(communityDao.getSearchCommunityList());	//검색용 정보
		//----------------------------------------------------------------------------------------
		//******************************완료
		return mapping.findForward("form");
	}
	
}
